/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wat.serwerapp.connection;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.ssl.SslFilter;
import wat.serwerapp.main.AccessControler;
import wat.ziszcommon.transport.Message;
import wat.ziszcommon.transport.MessageMethod;
import wat.ziszcommon.transport.MessagePKI;
import wat.ziszcommon.transport.MessageUsrSession;
import wat.ziszcommon.transport.MethodCallMessage;

/**
 *
 * @author Krzysztof Wilkos
 */
public class MessageHandler implements IoHandler {

    class UnknownMessageException extends IOException {
    }

    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("Session " + session.getId() + " created");
        session.setAttribute(SslFilter.USE_NOTIFICATION);
    }

    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("Session " + session.getId() + " opened");
    }

    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("Session " + session.getId() + " closed");
    }

    public void sessionIdle(IoSession session, IdleStatus idleStatus) throws Exception {
        System.out.println("Session " + session.getId() + " idle");
    }

    public void exceptionCaught(IoSession session, Throwable throwable) throws Exception {
        System.out.println("Exception caught at session " + session.getId() + ": " + throwable.getLocalizedMessage());
    }

    public void messageReceived(IoSession session, Object msg) throws Exception {
        System.out.println("Session " + session.getId() + " received message: " + msg.getClass().getSimpleName());
        Message m = (Message) msg;
        if (m.getClass().equals(MessageUsrSession.class)) {
            MessageUsrSession mUserAction = (MessageUsrSession) m;
            MessageUsrSession.Action act = mUserAction.getAction();
            switch (act) {
                case LOGIN:
                    session.write(Receiver.login(/*sessionID, */mUserAction.getUserName(), mUserAction.getPassword()));
                    return;
            }
            session.write("not OK");
        } else if (m.getClass().equals(MethodCallMessage.class)) {
            try {
                session.write(AccessControler.getInstance().processMethodCall((MethodCallMessage) m));
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(MessageHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            throw new UnknownMessageException();
        }
    }

    public void messageSent(IoSession session, Object msg) throws Exception {
        System.out.println("Session " + session.getId() + " sent message: " + msg);
    }
}
